package com.yoomiito.app.utils

import android.content.ContentValues
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.media.MediaScannerConnection
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.provider.MediaStore
import mtopsdk.network.util.Constants
import java.io.File
import java.io.FileInputStream
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.IOException


object FileUtils {
    /**
     * 创建需要保存的文件
     * @param isUseExternalFilesDir 是否使用getExternalFilesDir,false为保存在sdcard根目录下
     * @param fileName 保存文件名
     * @param folderName 保存在sdcard根目录下的文件夹名（isUseExternalFilesDir=false时需要）
     */


    public  fun createSaveFile(
        context: Context,
        source: File?,
        type:Int,
        fileName: String,
        folderName: String? = ""
    ){
        val appDir : File;
        if(type == 1){
             appDir = File(context.externalCacheDir, folderName)
        }else{
            appDir = File( Environment.getExternalStorageDirectory().path, "/DCIM/video_")
        }
        if (!appDir.exists()) {
            appDir.mkdir()
        }
        val target = File(appDir, fileName)
        var fileInputStream: FileInputStream? = null
        var fileOutputStream: FileOutputStream? = null
        try {
            fileInputStream = FileInputStream(source)
            fileOutputStream = FileOutputStream(target)
            val buffer = ByteArray(1024)
            while (fileInputStream.read(buffer) > 0) {
                fileOutputStream.write(buffer)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        } finally {
            try {
                source?.delete()
                fileInputStream?.close()
                fileOutputStream?.close()
            } catch (e: IOException) {
                e.printStackTrace()
            }
        }
        if(type == 1){
            MediaStore.Images.Media.insertImage(
                context.contentResolver,
                target.absolutePath,
                fileName,
                null
            )
        }else{

        }
        context.sendBroadcast(
            Intent(
                Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(
                    File(target.path)
                )
            )
        )
    }


    //保存Bitmap至本地
     fun saveBitmap2SelfDirectroy(
        context: Context,
        bitmap: Bitmap,
        file: File
    ) {
        try {
            val fos = FileOutputStream(file)
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos)
            fos.flush()
            fos.close()
        } catch (e: FileNotFoundException) {
            e.printStackTrace()
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }


    fun getVideoContentValues(
        paramContext: Context?,
        paramFile: File,
        paramLong: Long
    ): ContentValues {
        val localContentValues = ContentValues()
        localContentValues.put("title", paramFile.name)
        localContentValues.put("_display_name", paramFile.name)
        localContentValues.put("mime_type", "video/mp4")
        localContentValues.put("_data", paramFile.absolutePath)
        return localContentValues
    }

     fun copy(source: File?, target: File?) {
        var fileInputStream: FileInputStream? = null
        var fileOutputStream: FileOutputStream? = null
        try {
            fileInputStream = FileInputStream(source)
            fileOutputStream = FileOutputStream(target)
            val buffer = ByteArray(1024)
            while (fileInputStream.read(buffer) > 0) {
                fileOutputStream.write(buffer)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        } finally {
            try {
                source?.delete()
                fileInputStream?.close()
                fileOutputStream?.close()
            } catch (e: IOException) {
                e.printStackTrace()
            }
        }
    }
}